# ggrepel learning

library(ggrepel)
library(ggplot2)

# Scatter plot----

# Scatter labels of ggplot2
ggplot(mtcars) +
  geom_point(aes(wt, mpg), colour = 'red') +
  geom_text(aes(wt, mpg, label = rownames(mtcars))) +
  theme_classic(base_size = 16)

# for ggrepel
set.seed(42)
ggplot(mtcars) +
  geom_point(aes(wt, mpg), colour = 'red') +
  geom_text_repel(aes(wt, mpg, label = rownames(mtcars))) +
  theme_classic(base_size = 16)

# geom_label_repel() is based on geom_label(). It put labels in a box.
ggplot(mtcars) + geom_point(aes(wt, mpg), color = "grey", size = 5) +
  geom_label_repel(aes(wt, mpg, fill = factor(cyl), label = rownames(mtcars)),
    fontface = "bold", color = "white", box.padding = unit(0.35, "lines"),
    point.padding = unit(0.5, "lines"), segment.colour = "grey50") + 
  theme_classic(base_size = 16)

# Parameters
ggplot(mtcars) + geom_point(aes(wt, mpg, color = factor(cyl)), size = 3) +
  geom_text_repel(aes(wt, mpg, color = factor(cyl), 
                      label = rownames(mtcars), angle = ifelse(mtcars$cyl == 4, 90, 0)), 
                  size = 4, family = "Times", fontface = "bold", 
                  box.padding = unit(0.5, "lines"), point.padding = unit(1.6, "lines"), 
                  segment.color = "#cccccc", segment.size = 0.5, 
                  arrow = arrow(length = unit(0.01, "npc")),force = 1, max.iter = 3e3, 
                  nudge_x = ifelse(mtcars$cyl == 6, 2, 0), nudge_y = ifelse(mtcars$cyl == 6, 9, 0)) + 
  scale_color_discrete(name = "cyl") + 
  scale_x_continuous(expand = c(0.5, 0)) + 
  scale_y_continuous(expand = c(0.25, 0)) + 
  theme_classic(base_size = 16)

# 'point.padding = NA', means NOT repel
mtcars$label <- rownames(mtcars)
ggplot(mtcars, aes(wt, mpg, label = label)) + 
  geom_point(color = "red") + geom_text_repel(point.padding = NA) +
  theme_classic(base_size = 16)

# Set empty string ("") to hide some labels
mtcars$label[1:15] <- ""
ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(color = factor(cyl)), size = 2) +
  geom_text_repel(aes(color = factor(cyl), size = hp, label = label), 
                  point.padding = unit(0.25, "lines"), box.padding = unit(0.25, "lines"), 
                  nudge_y = 0.1) + theme_classic(base_size = 16)

# Confine labels in specific area by 'xlim' and 'ylim'
data <- mtcars
mu <- mean(data$wt)
left <- data[data$wt < mu, ]
right <- data[data$wt >= mu, ]
ggplot() + geom_vline(xintercept = mu) + 
  geom_point(aes(wt, mpg), data = data) + 
  geom_text_repel(data = left, aes(wt, mpg, label = rownames(left), 
                                 color = "Left half"), xlim = c(NA, mu)) + 
  geom_text_repel(data = right, aes(wt, mpg, label = rownames(right), 
                                  color = "Rigth half"), 
                  xlim = c(mu, NA)) + 
  theme_classic(base_size = 16)

# 'direction' can set the moving of label:
#   'both' is default;
#   'x', mean horizontal moving
ggplot(mtcars) +
  geom_point(aes(wt, mpg), color = "red") +
  geom_text_repel(aes(wt, mpg, label = rownames(mtcars)), direction = "x") +
  theme_classic(base_size = 16) + xlim(1, 6)

ggplot(mtcars) +
  geom_point(aes(wt, mpg), color = "red") +
  geom_text_repel(aes(wt, mpg, label = rownames(mtcars)), direction = "y") +
  theme_classic(base_size = 16) + xlim(1, 6)

# Line plot----
ggplot(Orange, aes(age, circumference, color = Tree)) +
  geom_line() +
  coord_cartesian(xlim = c(min(Orange$age), max(Orange$age) + 90)) +
  geom_text_repel(data = subset(Orange, age == max(age)),
                  aes(label = paste("Tree", Tree)), size = 6, 
                  nudge_x = 45, segment.color = NA) +
  theme_classic(base_size = 16) +
  theme(legend.position = "none") +
  labs(title = "Orange Trees", x = "Age(days)", y = "Circumference(mm)")
